package ${package}.app.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.CollectionUtils;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;


@Configuration
public class WebConfig implements WebMvcConfigurer {
    /**
     * 拦截器
     */
    @Autowired(required = false)
    private List<HandlerInterceptor> handlerInterceptors;

    /**
     * 参数解析器
     */
    @Autowired(required = false)
    private List<HandlerMethodArgumentResolver> handlerMethodArgumentResolvers;

    /**
     * 将拦截器加入
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        if (!CollectionUtils.isEmpty(handlerInterceptors)) {
            handlerInterceptors.forEach(item -> registry.addInterceptor(item).addPathPatterns("/**"));
        }
    }

    /**
     * 将解析器加入
     * SpringMVC解析器用于解析request请求参数并绑定数据到Controller的入参上。
     * @param argumentResolvers
     */
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
        if (!CollectionUtils.isEmpty(handlerMethodArgumentResolvers)) {
            handlerMethodArgumentResolvers.forEach(item-> argumentResolvers.add(item));
        }
    }
}
